function main(argMap) {
    var data = argMap.get("data");
    JSLogger.debug("data:" + data);
    var session = argMap.get("session");
    //{{1234567890}}
    if (session.getCache().get("dev_id") == null) {
        return;
    }
    
    if (data.length() > 1024) {
        data.setLength(0);
        return;
    }
    while (handDataPackage(data,session) != -1);
}

function handDataPackage(buffer,session) {
    var endIndex = buffer.indexOf("0D0A");
    if (endIndex == -1) {
        return -1;
    }
    var cmd = buffer.substring(0, endIndex);
    buffer.delete(0, endIndex + 4);
    cmd = JSHexUtil.hexToString(cmd);
    if(cmd.indexOf("hi")!=-1){
        return 0;
    }
    JSLogger.info("received:" + cmd);
    cmd=cmd.replace("AT+DATA=","");
    var fields = cmd.split(",");
    if(fields.length!=11){
        return;
    }
    session.getCache().put("sensor_realtime_data",cmd+","+new Date().getTime())
    var dataMap=session.getCache().get("sensor_data");
    if(dataMap==null){
        dataMap = JSopUtil.newMap();
        session.getCache().put("sensor_data",dataMap);
    }
    var now=new Date();
    var key=now.getDate()+"-"+now.getHours()+"";
    hoursData=dataMap.get(key);
    if(hoursData==null || hoursData.length!=fields.length){
        dataMap.put(key,fields);
        var yesterday=new Date(now-24*60*60*1000);
        dataMap.remove(yesterday.getDate()+"-"+yesterday.getHours());
    }
    hoursData=dataMap.get(key);
    for(var i=0;i<fields.length;i++){
        hoursData[i]=((parseFloat(hoursData[i])+parseFloat(fields[i]))/2).toFixed(2);
    }
    JSLogger.info("history data :" + dataMap);
    return 0;
}
